﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EA;
using HSDc.EA.WorkflowDefinitionsTranformPlugIn.Resources;

namespace HSDc.EABoundary
{
    public class DiagramSupport :   DefinitionTransformer.RequiredIntf.IDiagramSupport
    {

        public DefinitionTransformer.DTO.DiagramElement GetElement(int diagramId, string resourceId, int elementId)
        {
            Element eaEle = (Element)EAResource.CurrentRepository(resourceId).GetElementByID(elementId);
            DefinitionTransformer.DTO.DiagramElement digEle = new DefinitionTransformer.DTO.DiagramElement();
            if (eaEle != null)
            {
                digEle.Id = eaEle.ElementID;
                digEle.Name = eaEle.Name;
                digEle.ParentId = eaEle.ParentID;

                if (eaEle.Type == "StateNode") {
                    if (eaEle.Subtype > 100)
                        digEle.Type = DefinitionTransformer.DTO.ElementType.EndPoint;
                    else
                        digEle.Type = DefinitionTransformer.DTO.ElementType.StartPoint;
                }
                if (eaEle.Type == "Action") digEle.Type = DefinitionTransformer.DTO.ElementType.Action;
                if (eaEle.Type == "Activity") digEle.Type = DefinitionTransformer.DTO.ElementType.Activity;
                if (eaEle.Type == "Decision") digEle.Type = DefinitionTransformer.DTO.ElementType.Decision;
                if (eaEle.Type == "Partition") digEle.Type = DefinitionTransformer.DTO.ElementType.Partition;

                if (eaEle.Type == "ActivityPartition") digEle.Type = DefinitionTransformer.DTO.ElementType.Partition;

                if (eaEle.Type == "Synchronization") {
                    if (GetConnectorQty(diagramId, resourceId, elementId) > 1)
                        digEle.Type = DefinitionTransformer.DTO.ElementType.Fork;
                    else
                        digEle.Type = DefinitionTransformer.DTO.ElementType.Join;
                    
                } 
                

            }


            return digEle;
        }

        public DefinitionTransformer.DTO.DiagramElement GetStartPointElement(int diagramId, string resourceId)
        {
      
            string sqlStr = @"  Select ConnectorID From t_diagramlinks where ConnectorID in (
                                    Select Connector_ID from t_connector where Start_Object_ID  in ( SELECT td.Object_ID from t_diagramobjects as td inner join t_object as o on td.Object_ID = o.Object_ID 
                                where td.Diagram_ID=" + diagramId.ToString() + " and o.Object_Type = 'StateNode' ) )  ";


            System.Data.DataView dv = QueryEARepository(sqlStr, resourceId);
        
            Connector conn = null;
            foreach (System.Data.DataRowView drv in dv)
            {
                conn = EAResource.CurrentRepository(resourceId).
                                                   GetConnectorByID(Int32.Parse(drv["ConnectorID"].ToString()));
            }


            DefinitionTransformer.DTO.DiagramElement rootObj = new DefinitionTransformer.DTO.DiagramElement();
            if (conn != null)
            {
                rootObj.Id = conn.ClientID;
                rootObj.Type = DefinitionTransformer.DTO.ElementType.StartPoint;
            }
           

            return rootObj;
  

        }

        public List<DefinitionTransformer.DTO.DiagramElement> GetConnector(int diagramId, string resourceId, int elementId)
        {
            //digaram link
            string sqlStr = @"  Select ConnectorID From t_diagramlinks 
                                        where DiagramID ="+ diagramId +" And  ConnectorID in ( Select Connector_ID from t_connector where Start_Object_ID = " + elementId + ")  ";

            System.Data.DataView dv = QueryEARepository(sqlStr, resourceId);

            List<DefinitionTransformer.DTO.DiagramElement> digElements = new List<DefinitionTransformer.DTO.DiagramElement>();
            foreach (System.Data.DataRowView drv in dv)
            {
                Connector conn = EAResource.CurrentRepository(resourceId).
                                                   GetConnectorByID(Int32.Parse(drv["ConnectorID"].ToString()));


                DefinitionTransformer.DTO.Connector tran = new DefinitionTransformer.DTO.Connector();
                if (conn != null)
                {
                    tran.Id = conn.ConnectorID;
                    tran.SourceElementId = conn.ClientID;
                    tran.TargetElementId = conn.SupplierID;
                    tran.Constraint = conn.TransitionGuard;

                    tran.Type = DefinitionTransformer.DTO.ElementType.Transition;
                    digElements.Add(tran);

                }

       
            }

            return digElements;
        }


        private System.Data.DataView QueryEARepository(string sqlStr, string resourceId) 
        {

            string valueXml = EAResource.CurrentRepository(resourceId).SQLQuery(sqlStr);

            System.Data.DataSet tmpDs = new System.Data.DataSet();
            tmpDs.ReadXml(new System.IO.StringReader(valueXml));
            System.Data.DataView dv = new System.Data.DataView(tmpDs.Tables["Row"]);

            return dv;

        }

        private int GetConnectorQty(int diagramId, string resourceId, int elementId)
        {
            //digaram link
            string sqlStr = @"  Select ConnectorID From t_diagramlinks 
                                        where DiagramID =" + diagramId + " And  ConnectorID in ( Select Connector_ID from t_connector where Start_Object_ID = " + elementId + ")  ";

            System.Data.DataView dv = QueryEARepository(sqlStr, resourceId);

            return dv.Count;
        }


    }
}
